<template>
	<view class="grid col-4 grid-square flex-sub">
		<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
			<image :src="imgList[index]" mode="aspectFill"></image>
			<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
				<text class='cuIcon-close'></text>
			</view>
		</view>
		<view class="solids" @tap="ChooseImage" v-if="imgList.length<4">
			<text class='cuIcon-cameraadd'></text>
		</view>
	</view>
</template>

<script>
	import CommonApi from "@/api/common"
	export default {
		props: ['vlaue', 'max'],
		data() {
			return {
				imgList: [],
			}
		},
		methods: {
			ChooseImage() {
				let len = this.max ? parseInt(this.max) : 9
				uni.chooseImage({
					count: len, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						CommonApi.upload(res.tempFilePaths[0]).then((res) => {
							this.imgList.push(res)
							this.saveImgs()
						})
					}
				});
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					title: '提示',
					content: '确定要删除这张图片吗？',
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
							this.saveImgs()
						}
					}
				})
			},
			saveImgs(){
				this.$emit('input', this.imgList)
			}
		}
	}
</script>

<style>

</style>
